/**
 * 
 */
package com.oron3.bouncer.objects;

import javax.microedition.khronos.opengles.GL10;

import com.oron3.bouncer.Triangle;
import com.oron3.bouncer.genericgl.GL3D;
import com.oron3.bouncer.genericgl.GLWorld;
import com.oron3.bouncer.genericgl.MatrixTrackingGL;
import com.oron3.bouncer.objects.shapes.PPoint;
import com.oron3.bouncer.objects.shapes.Point;
import com.oron3.bouncer.objects.shapes.Sphere;


/**
 * @author Ofek
 *
 */
public class GLSphere extends Sphere implements GLShape {

	private GL3D gl3d;


	/**
	 * @param x
	 * @param y
	 * @param z
	 * @param radius
	 * @param f
	 */



	public GLSphere(final Point center, final float r, final float da) {
		super(center, r);
		init(da);
	}


	public GLSphere(final float x, final float y, final float z, final float radius, final float da) {
		super(x, y, z, radius);
		init(da);
	}




	/**
	 * @param da 
	 * 
	 */
	private void init(final float da) {
		gl3d = new GL3D();
		final PPoint traveler = new PPoint(r,0,0);
		for ( float a = 0 ; a < 2*Math.PI ; a += da) {
			traveler.a = a;
			for ( float b = 0 ; b < Math.PI ; b += da ) {
				traveler.b = b;
				gl3d.points.add(traveler.toCartesian());

			}
		}
		gl3d.prepare();
	}


	private void makeRectangle(final float da,final PPoint p,final Triangle triangle1,final Triangle triangle2) {	
		final float radius = p.r;
		final float a = p.a;
		final float b = p.b;

		p.toCartesian(triangle1.p1);
		p.set(radius, a + da, b);
		p.toCartesian(triangle1.p2);
		p.set(radius, a, b + da);
		p.toCartesian(triangle1.p3);

		p.set(radius, a + da, b + da);
		p.toCartesian(triangle2.p1);
		//		p.set(radius, a + da, b);
		//		p.toCartesian(triangle2.p2);
		//		p.set(radius, a, b + da);
		//		p.toCartesian(triangle2.p3);
		p.set(radius, a, b);
	}


	@Override
	public void rotate(final float angle) {
		this.angle = angle;
	}


	/* (non-Javadoc)
	 * @see com.oron3.bouncer.objects.GLObject#draw(com.oron3.bouncer.genericgl.MatrixTrackingGL)
	 */
	@Override
	public void draw(final MatrixTrackingGL gl) {
		gl.glColor4f(0.5f, 0.5f, 1f, 1f);
		gl.glPushMatrix();
		if ( angle!=0 ) 
			gl.glRotatef(angle, 0, 0, 1);
		gl.glTranslatef(center.x, center.y, center.z);
		gl3d.draw(gl,GL10.GL_POINTS);
		gl.glTranslatef(-center.x, -center.y, -center.z);
		if ( angle!=0 ) 
			gl.glRotatef(angle, 0, 0, 1);
		gl.glPopMatrix();
	}







	/* (non-Javadoc)
	 * @see com.oron3.bouncer.objects.GLShape#setColor(com.oron3.bouncer.objects.Color)
	 */
	@Override
	public void setColor(final Color c) {
	}


	/* (non-Javadoc)
	 * @see com.oron3.bouncer.objects.GLShape#step(float, com.oron3.bouncer.genericgl.GLWorld)
	 */
	@Override
	public void step(final float dt, final GLWorld glWorld) {
	}
}
